package autocode

import (
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/autocode"
	autocodeReq "github.com/flipped-aurora/gin-vue-admin/server/model/autocode/request"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/request"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/response"
	"github.com/flipped-aurora/gin-vue-admin/server/service"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

type DzxApi struct {
}

var dzxService = service.ServiceGroupApp.AutoCodeServiceGroup.DzxService

// CreateDzx 创建Dzx
// @Tags Dzx
// @Summary 创建Dzx
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body autocode.Dzx true "创建Dzx"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /dzx/createDzx [post]
func (dzxApi *DzxApi) CreateDzx(c *gin.Context) {
	var dzx autocode.Dzx
	_ = c.ShouldBindJSON(&dzx)
	if err := dzxService.CreateDzx(dzx); err != nil {
		global.GVA_LOG.Error("创建失败!", zap.Any("err", err))
		response.FailWithMessage("创建失败", c)
	} else {
		response.OkWithMessage("创建成功", c)
	}
}

// DeleteDzx 删除Dzx
// @Tags Dzx
// @Summary 删除Dzx
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body autocode.Dzx true "删除Dzx"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"删除成功"}"
// @Router /dzx/deleteDzx [delete]
func (dzxApi *DzxApi) DeleteDzx(c *gin.Context) {
	var dzx autocode.Dzx
	_ = c.ShouldBindJSON(&dzx)
	if err := dzxService.DeleteDzx(dzx); err != nil {
		global.GVA_LOG.Error("删除失败!", zap.Any("err", err))
		response.FailWithMessage("删除失败", c)
	} else {
		response.OkWithMessage("删除成功", c)
	}
}

// DeleteDzxByIds 批量删除Dzx
// @Tags Dzx
// @Summary 批量删除Dzx
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.IdsReq true "批量删除Dzx"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"批量删除成功"}"
// @Router /dzx/deleteDzxByIds [delete]
func (dzxApi *DzxApi) DeleteDzxByIds(c *gin.Context) {
	var IDS request.IdsReq
	_ = c.ShouldBindJSON(&IDS)
	if err := dzxService.DeleteDzxByIds(IDS); err != nil {
		global.GVA_LOG.Error("批量删除失败!", zap.Any("err", err))
		response.FailWithMessage("批量删除失败", c)
	} else {
		response.OkWithMessage("批量删除成功", c)
	}
}

// UpdateDzx 更新Dzx
// @Tags Dzx
// @Summary 更新Dzx
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body autocode.Dzx true "更新Dzx"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"更新成功"}"
// @Router /dzx/updateDzx [put]
func (dzxApi *DzxApi) UpdateDzx(c *gin.Context) {
	var dzx autocode.Dzx
	_ = c.ShouldBindJSON(&dzx)
	if err := dzxService.UpdateDzx(dzx); err != nil {
		global.GVA_LOG.Error("更新失败!", zap.Any("err", err))
		response.FailWithMessage("更新失败", c)
	} else {
		response.OkWithMessage("更新成功", c)
	}
}

// FindDzx 用id查询Dzx
// @Tags Dzx
// @Summary 用id查询Dzx
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query autocode.Dzx true "用id查询Dzx"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"查询成功"}"
// @Router /dzx/findDzx [get]
func (dzxApi *DzxApi) FindDzx(c *gin.Context) {
	var dzx autocode.Dzx
	_ = c.ShouldBindQuery(&dzx)
	if err, redzx := dzxService.GetDzx(dzx.ID); err != nil {
		global.GVA_LOG.Error("查询失败!", zap.Any("err", err))
		response.FailWithMessage("查询失败", c)
	} else {
		response.OkWithData(gin.H{"redzx": redzx}, c)
	}
}

// GetDzxList 分页获取Dzx列表
// @Tags Dzx
// @Summary 分页获取Dzx列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query autocodeReq.DzxSearch true "分页获取Dzx列表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /dzx/getDzxList [get]
func (dzxApi *DzxApi) GetDzxList(c *gin.Context) {
	var pageInfo autocodeReq.DzxSearch
	_ = c.ShouldBindQuery(&pageInfo)
	if err, list, total := dzxService.GetDzxInfoList(pageInfo); err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Any("err", err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithDetailed(response.PageResult{
			List:     list,
			Total:    total,
			Page:     pageInfo.Page,
			PageSize: pageInfo.PageSize,
		}, "获取成功", c)
	}
}
